<?php 
	$fileInName = "book.txt";
	//$fileOutName = "newbook.txt";

	$strTable1 = "第";
	
	$fileIn = fopen($fileInName, 'r');
	//$fileOut = fopen($fileOutName, 'a');

	while (!feof($fileIn)) {
		$fileRow = fgets($fileIn);
		$fileRow = preg_replace("/^\r?\n/","",$fileRow);		//删除空行
		$fileRow = rtrim($fileRow, " ");						//删除末尾空格
		$pos = mb_strpos($fileRow, $strTable1, 0, 'UTF-8');		//查找中文
		
		//按每章节创建txt文件
		if($pos === 0) {
			$fileRow = trim($fileRow);							//删除换行符,否则不能创建文件
			$fileRow = iconv('UTF-8', 'GBK', $fileRow);
			$file = $fileRow.".txt";
			$newfilename = fopen($file, 'a');
			$fileRow = $fileRow."\r\n";							//插入换行符,让标题独自一行
			fwrite($newfilename, $fileRow);
		}
		//插当前入章节内容
 		$fileRow = iconv('UTF-8', 'GBK', $fileRow);
		fwrite($newfilename, $fileRow);
		
	}
	
	fclose($newfilename);
	fclose($fileIn);